﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using BUS;

namespace QuanLyKhachSan
{
    public partial class TraPhong : Form
    {

        public TraPhong()
        {
            InitializeComponent();
        }

        private void buttonTraCuuCB_Click(object sender, EventArgs e)
        {

        }

        private void btn_KiemTra_Click(object sender, EventArgs e)
        {
            string tenKH = txt_KhachHang.Text, cmnd = txt_CMND.Text, maKH="";
            
                if (tenKH != "" && cmnd != "")
                {
                    maKH = BUS.KhachHangBUS.layDSMaKHTheoTenVaCMND(tenKH, cmnd).ToString();
                    txt_MaKH.Text = maKH;
                }
                else
                    MessageBox.Show("Bạn chưa nhập đủ thông tin !!!", "Thông báo !!!");


                List<PhongDTO> dsPhongDaThue = BUS.PhongBUS.layDSPhongTheoMaKH(maKH);
                for (int i = 0; i < dsPhongDaThue.Count; i++)
                {
                    if (dsPhongDaThue[i].TinhTrang == 1)
                    {
                        ListViewItem item = new ListViewItem(dsPhongDaThue[i].MaPhong.ToString(), i);
                        item.SubItems.Add(dsPhongDaThue[i].LoaiPhong.TenLoaiPhong);
                        item.SubItems.Add(BUS.PhongBUS.LayNgayBatDauThue(maKH, dsPhongDaThue[i].MaPhong.ToString()));
                        item.SubItems.Add(BUS.PhongBUS.laySoNgayThue(maKH, dsPhongDaThue[i].MaPhong.ToString()).ToString());
                        item.SubItems.Add(dsPhongDaThue[i].LoaiPhong.DonGia.ToString());
                        item.SubItems.Add(BUS.PhongBUS.TinhTien(maKH, dsPhongDaThue[i].MaPhong.ToString(), BUS.PhongBUS.laySoNgayThue(maKH, dsPhongDaThue[i].MaPhong.ToString())).ToString());
                        list_PhongDaThue.Items.Add(item);
                        btLapHoaDon.Enabled = true;
                    }
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double thanhtien = 0;
            List<string> ListMaPhong = new List<string>();
            List<string> ListNgayLap = new List<string>();
            DateTime now = DateTime.Now;
            for (int i = 0; i < list_PhongDaThue.Items.Count; i++)
            {
                if (list_PhongDaThue.Items[i].Checked == true)
                {
                    thanhtien += Convert.ToDouble(list_PhongDaThue.Items[i].SubItems[5].Text.ToString());

                    ListMaPhong.Add(list_PhongDaThue.Items[i].Text);
                    ListNgayLap.Add(list_PhongDaThue.Items[i].SubItems[2].Text);
                    long maphong = Convert.ToInt32(list_PhongDaThue.Items[i].Text);
                    string tenloaiphong = list_PhongDaThue.Items[i].SubItems[1].Text;
                    string ngaybd = list_PhongDaThue.Items[i].SubItems[2].Text;
                    int songay = Convert.ToInt16(list_PhongDaThue.Items[i].SubItems[3].Text);
                    double tien = Convert.ToDouble(list_PhongDaThue.Items[i].SubItems[5].Text.ToString());
                    BUS.BaoCaoBUS.themBaoCao(maphong, tenloaiphong, ngaybd, songay, tien, now.ToString());
                }

            }

            ListViewItem item = new ListViewItem(now.ToString(), 0);
            item.SubItems.Add(thanhtien.ToString());
            list_HoaDon.Items.Add(item);

            // them hoa don voa csdl
            BUS.HoaDonBUS.themHoaDon(now, thanhtien, txt_MaKH.Text);
            for (int i = 0; i < ListMaPhong.Count; i++)
            {
                long maHD = BUS.HoaDonBUS.layMaHDTheoMaKHvaNgayLap(txt_MaKH.Text, now);
                long maPT = BUS.PhieuThueBUS.layMaPTTheoMaPhongvaNgayBDThue(ListMaPhong[i], DateTime.Parse(ListNgayLap[i]));
                long maCTPT = BUS.ChiTietPhieuThueBUS.layMaCTPTTheoMaKHvaMaPhieuThue(txt_MaKH.Text, maPT);
                BUS.ChiTietHoaDonBUS.themChiTietHoaDon(maHD, maCTPT);
            }

            List<PhongDTO> dsPhong = PhongBUS.layDSPhong();
            for (int i = 0; i < dsPhong.Count; i++)
            {
                for (int j = 0; j < ListMaPhong.Count; j++)
                {
                    if (dsPhong[i].MaPhong == Convert.ToInt32(ListMaPhong[j]))
                    {
                        PhongBUS.updatePhong(dsPhong[i].MaPhong, dsPhong[i].TenPhong, dsPhong[i].LoaiPhong.MaLoaiPhong, 0, dsPhong[i].GhiChu);
                    }
                }
            }

            MessageBox.Show("Lập hóa đơn thành công !!!", "Thông báo !!!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Chắc chắn muốn thoát ???", "Warning !!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                this.Close();
        }
    }
}
